import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtCharts 2.0
import QtQuick.Dialogs 1.2
import FileContentItem 1.0
ColumnLayout{

    function plotChart1() {
        spline1Label.text = dataFromCpp1.layer0
        spline1Label2.text = dataFromCpp1.layer1
        spline1Label3.text = dataFromCpp1.layer2
        spline1Label4.text = dataFromCpp1.layer3
        spline1.title = dataFromCpp1.chartName
        var maxSfr = 0

        if(displaySpline1Mode.checked) {
            for (var i=0; i<dataFromCpp1.wCCList.length; i=i+2) {
                ccSeries1.append(dataFromCpp1.wCCList[i], dataFromCpp1.wCCList[i+1])
                if (dataFromCpp1.wCCList[i+1] > maxSfr) {
                    maxSfr = dataFromCpp1.wCCList[i+1]
                }
            }
            for (i=0; i<dataFromCpp1.wULList.length; i=i+2) {
                ulSeries1.append(dataFromCpp1.wULList[i], dataFromCpp1.wULList[i+1])
            }
            for (i=0; i<dataFromCpp1.wURList.length; i=i+2) {
                urSeries1.append(dataFromCpp1.wURList[i], dataFromCpp1.wURList[i+1])
            }
            for (i=0; i<dataFromCpp1.wLLList.length; i=i+2) {
                llSeries1.append(dataFromCpp1.wLLList[i], dataFromCpp1.wLLList[i+1])
            }
            for (i=0; i<dataFromCpp1.wLRList.length; i=i+2) {
                lrSeries1.append(dataFromCpp1.wLRList[i], dataFromCpp1.wLRList[i+1])
            }
        } else {
            for (var i=0; i<dataFromCpp1.wCCRealList.length; i=i+2) {
                ccSeries1.append(dataFromCpp1.wCCRealList[i], dataFromCpp1.wCCRealList[i+1])
                if (dataFromCpp1.wCCRealList[i+1] > maxSfr) {
                    maxSfr = dataFromCpp1.wCCRealList[i+1]
                }
            }
            for (i=0; i<dataFromCpp1.wULRealList.length; i=i+2) {
                ulSeries1.append(dataFromCpp1.wULRealList[i], dataFromCpp1.wULRealList[i+1])
            }
            for (i=0; i<dataFromCpp1.wURRealList.length; i=i+2) {
                urSeries1.append(dataFromCpp1.wURRealList[i], dataFromCpp1.wURRealList[i+1])
            }
            for (i=0; i<dataFromCpp1.wLLRealList.length; i=i+2) {
                llSeries1.append(dataFromCpp1.wLLRealList[i], dataFromCpp1.wLLRealList[i+1])
            }
            for (i=0; i<dataFromCpp1.wLRRealList.length; i=i+2) {
                lrSeries1.append(dataFromCpp1.wLRRealList[i], dataFromCpp1.wLRRealList[i+1])
            }
        }

        axisX1.min = ccSeries1.at(0).x
        axisX1.max = ccSeries1.at(ccSeries1.count - 1).x
        if (maxSfr > 80) {
            axisY1.max = 100
            zPeak1.append(dataFromCpp1.zPeak,0)
            zPeak1.append(dataFromCpp1.zPeak,100)
        } else if (maxSfr > 60) {
            axisY1.max = 80
            zPeak1.append(dataFromCpp1.zPeak,0)
            zPeak1.append(dataFromCpp1.zPeak,80)
        } else {
            axisY1.max = 60
            zPeak1.append(dataFromCpp1.zPeak,0)
            zPeak1.append(dataFromCpp1.zPeak,60)
        }
    }

    function clearPlot1(){
        ccSeries1.clear()
        ulSeries1.clear()
        urSeries1.clear()
        llSeries1.clear()
        lrSeries1.clear()
        zPeak1.clear()
        spline1Label.text = ""
        spline1Label2.text = ""
        spline1Label3.text = ""
        spline1Label4.text = ""
    }

    function plotChart2() {
        spline2Label.text = dataFromCpp2.layer0
        spline2Label2.text = dataFromCpp2.layer1
        spline2Label3.text = dataFromCpp2.layer2
        spline2Label4.text = dataFromCpp2.layer3
        spline2.title = dataFromCpp2.chartName
        var maxSfr = 0

        if(displaySpline2Mode.checked) {
            for (var i=0; i<dataFromCpp2.wCCList.length; i=i+2) {
                ccSeries2.append(dataFromCpp2.wCCList[i], dataFromCpp2.wCCList[i+1])
                if (dataFromCpp2.wCCList[i+1] > maxSfr) {
                    maxSfr = dataFromCpp2.wCCList[i+1]
                }
            }
            for (i=0; i<dataFromCpp2.wULList.length; i=i+2) {
                ulSeries2.append(dataFromCpp2.wULList[i], dataFromCpp2.wULList[i+1])
            }
            for (i=0; i<dataFromCpp2.wURList.length; i=i+2) {
                urSeries2.append(dataFromCpp2.wURList[i], dataFromCpp2.wURList[i+1])
            }
            for (i=0; i<dataFromCpp2.wLLList.length; i=i+2) {
                llSeries2.append(dataFromCpp2.wLLList[i], dataFromCpp2.wLLList[i+1])
            }
            for (i=0; i<dataFromCpp2.wLRList.length; i=i+2) {
                lrSeries2.append(dataFromCpp2.wLRList[i], dataFromCpp2.wLRList[i+1])
            }
        } else {
            for (var i=0; i<dataFromCpp2.wCCRealList.length; i=i+2) {
                ccSeries2.append(dataFromCpp2.wCCRealList[i], dataFromCpp2.wCCRealList[i+1])
                if (dataFromCpp2.wCCRealList[i+1] > maxSfr) {
                    maxSfr = dataFromCpp2.wCCRealList[i+1]
                }
            }
            for (i=0; i<dataFromCpp2.wULRealList.length; i=i+2) {
                ulSeries2.append(dataFromCpp2.wULRealList[i], dataFromCpp2.wULRealList[i+1])
            }
            for (i=0; i<dataFromCpp2.wURRealList.length; i=i+2) {
                urSeries2.append(dataFromCpp2.wURRealList[i], dataFromCpp2.wURRealList[i+1])
            }
            for (i=0; i<dataFromCpp2.wLLRealList.length; i=i+2) {
                llSeries2.append(dataFromCpp2.wLLRealList[i], dataFromCpp2.wLLRealList[i+1])
            }
            for (i=0; i<dataFromCpp2.wLRRealList.length; i=i+2) {
                lrSeries2.append(dataFromCpp2.wLRRealList[i], dataFromCpp2.wLRRealList[i+1])
            }
        }

        axisX2.min = ccSeries2.at(0).x
        axisX2.max = ccSeries2.at(ccSeries2.count - 1).x
        if (maxSfr > 80) {
            axisY2.max = 100
            zPeak2.append(dataFromCpp2.zPeak,0)
            zPeak2.append(dataFromCpp2.zPeak,100)
        } else if (maxSfr > 60) {
            axisY2.max = 80
            zPeak2.append(dataFromCpp2.zPeak,0)
            zPeak2.append(dataFromCpp2.zPeak,80)
        } else {
            axisY2.max = 60
            zPeak2.append(dataFromCpp2.zPeak,0)
            zPeak2.append(dataFromCpp2.zPeak,60)
        }
    }

    function clearPlot2(){
        ccSeries2.clear()
        ulSeries2.clear()
        urSeries2.clear()
        llSeries2.clear()
        lrSeries2.clear()
        zPeak2.clear()
        spline2Label.text = ""
        spline2Label2.text = ""
        spline2Label3.text = ""
        spline2Label4.text = ""
    }

    FileDialog {
        id: loadIniDialog
        title: qsTr("选择加载文件")
        selectExisting: true
        selectFolder: false
        selectMultiple: false

        nameFilters: ["ini文件 (*.ini)"]
        onAccepted: {
            aaCoreConfig.setSensorIniFilename(loadIniDialog.fileUrl)
        }
    }


    ScrollView {
        implicitWidth:  1000
        implicitHeight: 800
        clip: true
        ColumnLayout {
            RowLayout {
                Button {
                    text: qsTr("Open Camera")
                    onClicked: {
                        tem.runSingleCmd(aaCore, "performInitCameraMaunal")
                    }
                }
                Button {
                    text: qsTr("Grab Image")
                    onClicked: {
                        tem.runSingleCmd(aaCore, "performGrabImage")
                    }
                }
                Button {
                    text: qsTr("Close Camera")
                    //enabled: aaCore.liveImageDisplayStatus
                    onClicked: {
                        tem.runSingleCmd(aaCore, "performCloseCamera")
                    }
                }

                ProgressBar {
                    id: iniSensorStatus
                    width: 30
                    indeterminate: aaCore.liveImageDisplayStatus
                }
            }

            RowLayout {
                Button {
                    text: qsTr("OC")
                    onClicked: {
                        tem.runSingleCmd(aaCore, "performOCManual")
                    }
                }
                Button {
                    text: qsTr("MTF")
                    onClicked: {
                        tem.runSingleCmd(aaCore, "performMTFManual")
                    }
                }
                Button {
                    text: qsTr("AA Test")
                    onClicked: {
                        tem.runSingleCmd(aaCore, "performAATest")
                    }
                }
            }

            GroupBox {
                title: qsTr("AA Core")
                Grid {
                    id: grid
                    columns: 3
                    rows: 2
                    width: 1000
                    height: 1000
                    Frame {
                        width: grid.width/2
                        height: grid.height/2
                        Label { text: qsTr("Live") }
                        Image {
                            id: liveImage
                            anchors.fill: parent
                            fillMode: Image.PreserveAspectFit
                            cache: false
                            Connections {
                                target: aaImage
                                onDisplayLiveImage: {
                                    liveImage.source = ""
                                    liveImage.source = "image://liveImage"
                                }
                            }
                            Rectangle {
                                color: "pink"
                                opacity: 0.8
                                x: (liveImage.width - liveImage.paintedWidth)/2
                                y: liveImage.height/2
                                width: liveImage.paintedWidth
                                height: 1
                            }
                            Rectangle {
                                color: "pink"
                                opacity: 0.8
                                x: liveImage.width/2
                                y: (liveImage.height - liveImage.paintedHeight)/2
                                width: 1
                                height: liveImage.paintedHeight
                            }

                        }
                    }
                    Frame {
                        width: grid.width/2
                        height: grid.height/2
                        Label { text: qsTr("MTF") }
                        Image {
                            id: image1
                            anchors.fill: parent
                            fillMode: Image.PreserveAspectFit
                            cache: false
                            Connections {
                                target: aaImage
                                onDisplayMTFImage: {
                                    image1.source = ""
                                    image1.source = "image://mtfImage"
                                }
                            }
                        }
                    }
                    Frame {
                        width: grid.width/2
                        height: grid.height/2
                        Label { text: qsTr("OC") }
                        Image {
                            id: ocImage
                            anchors.fill: parent
                            fillMode: Image.PreserveAspectFit
                            cache: false
                            Connections {
                                target: aaImage
                                onDisplayOCImage: {
                                    ocImage.source = ""
                                    ocImage.source = "image://ocImage"
                                }
                            }
                            Rectangle {
                                color: "pink"
                                opacity: 0.8
                                x: (ocImage.width - ocImage.paintedWidth)/2
                                y: ocImage.height/2
                                width: ocImage.paintedWidth
                                height: 1
                            }
                            Rectangle {
                                color: "pink"
                                opacity: 0.8
                                x: ocImage.width/2
                                y: (ocImage.height - ocImage.paintedHeight)/2
                                width: 1
                                height: ocImage.paintedHeight
                            }

                        }
                    }
                    Frame {
                        id: frame4
                        width: grid.width/2
                        height: grid.height/2
                        ChartView {
                            id: spline1
                            anchors.fill: parent
                            title: "AA "
                            antialiasing: true
                            theme: ChartView.ChartThemeDark
                            legend.alignment: Qt.AlignBottom
                            margins.top: 50
                            ValueAxis {
                                id: axisY1
                                titleText: "SFR"
                                min: 0
                                max: 60
                                tickCount: 13
                                gridVisible: true
                            }
                            ValueAxis {
                                titleText: "z (um)"
                                id: axisX1
                                tickCount: 7
                                gridVisible: true
                            }
                            LineSeries
                            {
                                name:"zPeak"
                                id:zPeak1
                                axisX: axisX1
                                axisY: axisY1
                            }
                            SplineSeries {
                                name: "CC "
                                id: ccSeries1
                                axisX: axisX1
                                axisY: axisY1
                            }
                            SplineSeries {
                                name: "UL "
                                id: ulSeries1
                                axisX: axisX1
                                axisY: axisY1
                            }
                            SplineSeries {
                                name: "UR "
                                id: urSeries1
                                axisX: axisX1
                                axisY: axisY1
                            }
                            SplineSeries {
                                name: "LL "
                                id: llSeries1
                                axisX: axisX1
                                axisY: axisY1
                            }
                            SplineSeries {
                                name: "LR "
                                id: lrSeries1
                                axisX: axisX1
                                axisY: axisY1
                            }
                        }
                        RowLayout {
                            anchors.right: parent.right
                            anchors.rightMargin: 8
                            anchors.left: parent.left
                            anchors.leftMargin: 8
                            anchors.bottom: parent.bottom
                            anchors.bottomMargin: 0
                            Switch {
                                id: displaySpline1Mode
                                font.pixelSize: 10
                                text: qsTr("Displaying Curve Data")
                                checked: true
                                onCheckedChanged: {
                                    if (checked) {
                                        text = qsTr("Displaying Curve Data")
                                    } else {
                                        text = qsTr("Displaying Raw Data")
                                    }
                                    clearPlot1()
                                    plotChart1()
                                }
                            }
                            ProgressBar {
                                id: spline1progressBar
                                font.pointSize: 16
                                Layout.rightMargin: 7
                                Layout.fillWidth: true
                            }
                        }
                        ColumnLayout {
                            Label {
                                id: spline1Label
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                            Label {
                                id: spline1Label2
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                            Label {
                                id: spline1Label3
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                            Label {
                                id: spline1Label4
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                        }
                    }
                    Connections {
                        target: dataFromCpp1
                        onWValueChanged: {
                            plotChart1()
                        }
                        onWValueClear: {
                            clearPlot1()
                        }
                        onWInProgressChanged: {
                            if (dataFromCpp1.inProgress) {
                                spline1progressBar.indeterminate = true
                            } else {
                                spline1progressBar.indeterminate = false
                            }
                        }
                    }
                    Frame {
                        id: frame5
                        width: grid.width/2
                        height: grid.height/2
                        ChartView {
                            id: spline2
                            anchors.fill: parent
                            title: "AA "
                            antialiasing: true
                            theme: ChartView.ChartThemeDark
                            legend.alignment: Qt.AlignBottom
                            margins.top: 50
                            ValueAxis {
                                id: axisY2
                                titleText: "SFR"
                                min: 0
                                max: 60
                                tickCount: 13
                                gridVisible: true
                            }
                            ValueAxis {
                                titleText: "z (um)"
                                id: axisX2
                                tickCount: 7
                                gridVisible: true
                            }
                            LineSeries
                            {
                                name:"zPeak"
                                id:zPeak2
                                axisX: axisX2
                                axisY: axisY2
                            }
                            SplineSeries {
                                name: "CC "
                                id: ccSeries2
                                axisX: axisX2
                                axisY: axisY2
                            }
                            SplineSeries {
                                name: "UL "
                                id: ulSeries2
                                axisX: axisX2
                                axisY: axisY2
                            }
                            SplineSeries {
                                name: "UR "
                                id: urSeries2
                                axisX: axisX2
                                axisY: axisY2
                            }
                            SplineSeries {
                                name: "LL "
                                id: llSeries2
                                axisX: axisX2
                                axisY: axisY2
                            }
                            SplineSeries {
                                name: "LR "
                                id: lrSeries2
                                axisX: axisX2
                                axisY: axisY2
                            }
                        }
                        RowLayout {
                            anchors.right: parent.right
                            anchors.rightMargin: 8
                            anchors.left: parent.left
                            anchors.leftMargin: 8
                            anchors.bottom: parent.bottom
                            anchors.bottomMargin: 0
                            Switch {
                                id: displaySpline2Mode
                                font.pixelSize: 10
                                text: qsTr("Displaying Curve Data")
                                checked: true
                                onCheckedChanged: {
                                    if (checked) {
                                        text = qsTr("Displaying Curve Data")
                                    } else {
                                        text = qsTr("Displaying Raw Data")
                                    }
                                    clearPlot2()
                                    plotChart2()
                                }
                            }
                            ProgressBar {
                                id: spline2progressBar
                                font.pointSize: 16
                                Layout.rightMargin: 7
                                Layout.fillWidth: true
                            }
                        }
                        ColumnLayout {
                            Label {
                                id: spline2Label
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                            Label {
                                id: spline2Label2
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                            Label {
                                id: spline2Label3
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                            Label {
                                id: spline2Label4
                                background: Rectangle {
                                    color: "black"
                                }
                                font.pixelSize: 10
                            }
                        }
                    }
                    Connections {
                        target: dataFromCpp2
                        onWValueChanged: {
                            plotChart2()
                        }
                        onWValueClear: {
                            clearPlot2()
                        }
                        onWInProgressChanged: {
                            if (dataFromCpp2.inProgress) {
                                spline2progressBar.indeterminate = true
                            } else {
                                spline2progressBar.indeterminate = false
                            }
                        }
                    }
                }
            }

            GroupBox {
                title: qsTr("MTF Loop Test")
                MTFLoopTestView {}
            }
        }
    }
}

